const express = require("express");
const query = require("../sql/mysql");
const Router = express.Router();
const {
    create,
    verify
} = require("./token");
//渲染功能
Router.get("/user/all", async(req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,PATCH,POST,GET,DELETE,OPTIONS");
    let page = req.query.page;
    page = page || 1;
    let num = 5;
    let index = (page - 1) * num;
    let sql = `SELECT * FROM zc LIMIT ${index},${num}`;
    let data = await query(sql);
    let sql1 = `SELECT * FROM zc`;
    let data1 = await query(sql1);
    let obj = {}
    if (data.length) {
        let totalPage = Math.ceil(data1.length / num);
        obj = {
            msg: "成功",
            page,
            totalPage,
            list: data
        };
    } else {
        obj = {
            msg: "失败",
        };
    }
    res.send(obj)
});

//检查用户名是否存在
Router.get("/user/checkname", async(req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,PATCH,POST,GET,DELETE,OPTIONS");
    let username = req.query.username;
    let sql = `SELECT * FROM zc WHERE username='${username}'`;
    let data = await query(sql);
    let obj = {};
    if (username) {
        if (data.length) {
            obj = {
                msg: "用户名已存在"
            }
        } else {
            obj = {
                msg: "用户名可用"
            }
        }
    } else {
        obj = {
            msg: "请输入用户名"
        }
    }
    res.send(obj);
});
//注册
Router.post("/user/reg", express.urlencoded(), express.json(), async(req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,PATCH,POST,GET,DELETE,OPTIONS");
    let username = req.body.username;
    let password = req.body.password;
    let email = req.body.email;
    let sql = `INSERT INTO zc (username,password,email)VALUES('${username}','${password}','${email}')`
    let data = await query(sql);
    let obj = {}
    if (data.affectedRows) {
        obj.msg = "注册成功";
    } else {
        obj.msg = "请填写所有数据";
    }
    res.send(obj);
});
//登录
Router.post("/user/login", express.urlencoded(), async(req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,PATCH,POST,GET,DELETE,OPTIONS");
    let username = req.body.username;
    let password = req.body.password;
    let sql = `SELECT * FROM zc WHERE username='${username}'`;
    let data = await query(sql);
    let obj = {};
    if (data.length != 0) {
        if (password == data[0].password) {
            obj.msg = "登录成功";
            obj.id = data[0].id;
            obj.token = create(password);
        } else {
            obj.msg = "密码错误"
        }
    } else {
        obj.msg = "用户名不存在，请先注册"
    }
    res.send(obj);
});
//删除单个
Router.delete("/user/delete", express.urlencoded(), async(req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,PATCH,POST,GET,DELETE,OPTIONS,DELETE");
    let id = req.body.id;
    let sql = `DELETE FROM zc WHERE id=${id}`
    let data = await query(sql);
    let obj = {}
    if (data.affectedRows) {
        obj.msg = "删除成功"
    } else {
        obj.msg = "删除失败"
    }
    res.send(obj);
});
//删除所有
Router.delete("/user/deleteall", express.urlencoded(), async(req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,PATCH,POST,GET,DELETE,OPTIONS");
    let id = req.body.id;
    let sql = `DELETE FROM zc WHERE id in(${id});`;
    let data = await query(sql);
    let obj = {}
    if (data.affectedRows) {
        obj.msg = "删除成功"
    } else {
        obj.msg = "删除失败"
    }
    res.send(obj)
});
//内容更改
Router.put("/user/update", express.urlencoded(), async(req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,PATCH,POST,GET,DELETE");
    let id = req.body.id;
    let putdata = req.body;
    let msg = "";
    for (let key in putdata) {
        if (key != "id") {
            msg += `${key}='${putdata[key]}',`
        }
    }
    msg = msg.slice(0, -1)

    let obj = {};
    if (msg != "") {
        let sql = `UPDATE zc SET ${msg} WHERE id=${id}`;
        let data = await query(sql);
        if (data.affectedRows) {
            obj.msg = "更改成功"
        } else {
            obj.msg = "更改失败"
        }
    } else {
        obj.msg = "请输入内容"
    }
    res.send(obj);

});

//核对token
Router.get("/user/checktoken", async(req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,PATCH,POST,GET,DELETE,OPTIONS");
    let token = req.query.token;
    let result = verify(token)
    let obj = {};
    if (result) {
        obj.msg = "已登录";
    } else {
        obj.msg = "未登录";
    }
    res.send(obj);
});
module.exports = Router;